route.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { listDays } from "@/lib/storage";
  2. import { getSession } from "@/lib/auth/session";
  3. import { canAccessBranch } from "@/lib/auth/permissions";
  4. import {
  5. withErrorHandling,
  6. json,
  7. badRequest,
  8. unauthorized,
  9. forbidden,
  10. } from "@/lib/api/errors";
  11. import { mapStorageReadError } from "@/lib/api/storageErrors";
  12. /**
  13. * Next.js Route Handler caching configuration (RHL-006):
  14. *
  15. * We force this route to execute dynamically on every request.
  16. *
  17. * Reasons:
  18. * - NAS contents can change at any time (new scans).
  19. * - Auth/RBAC-protected responses must not be cached/shared across users.
  20. * - We rely on a small storage-layer TTL micro-cache instead of Next route caching.
  21. */
  22. export const dynamic = "force-dynamic";
  23. /**
  24. * GET /api/branches/[branch]/[year]/[month]/days
  25. *
  26. * Happy-path response must remain unchanged:
  27. * { "branch":"NL01", "year":"2024", "month":"10", "days":["23", ...] }
  28. */
  29. export const GET = withErrorHandling(
  30. async function GET(request, ctx) {
  31. const session = await getSession();
  32. if (!session) {
  33. throw unauthorized("AUTH_UNAUTHENTICATED", "Unauthorized");
  34. }
  35. const { branch, year, month } = await ctx.params;
  36. const missing = [];
  37. if (!branch) missing.push("branch");
  38. if (!year) missing.push("year");
  39. if (!month) missing.push("month");
  40. if (missing.length > 0) {
  41. throw badRequest(
  42. "VALIDATION_MISSING_PARAM",
  43. "Missing required route parameter(s)",
  44. { params: missing }
  45. );
  46. }
  47. if (!canAccessBranch(session, branch)) {
  48. throw forbidden("AUTH_FORBIDDEN_BRANCH", "Forbidden");
  49. }
  50. try {
  51. const days = await listDays(branch, year, month);
  52. return json({ branch, year, month, days }, 200);
  53. } catch (err) {
  54. throw await mapStorageReadError(err, {
  55. details: { branch, year, month },
  56. });
  57. }
  58. },
  59. { logPrefix: "[api/branches/[branch]/[year]/[month]/days]" }
  60. );